home *** CD-ROM | disk | FTP | other *** search
- /*****
- *
- * Startup.c
- *
- * If you want to exclude CGI or ListSTAR support, remove the appropriate lines
- * from the 'StartupApplication' function.
- *
- * This is a support file for "Grant's CGI Framework".
- * Please see the license agreement that accompanies the distribution package
- * for licensing details.
- *
- * Copyright ©1995,1996 by Grant Neufeld
- * grant@acm.com
- * http://arpp.carleton.ca/grant/
- *
- *****/
-
- #include "MyConfiguration.h"
-
- #include <Threads.h>
- #include <Traps.h>
-
- #include "compiler_stuff.h"
- #include "constants.h"
- #include "globals.h"
-
- #include "AboutBox.h"
- #include "AEFunc.h"
- #include "AEHandlers.h"
- #include "CGI.h"
- #include "ErrorUtil.h"
- #include "EventUtil.h"
- #include "ListSTAR.h"
- #include "LogUtil.h"
- #include "MemoryUtil.h"
- #include "MenuFunc.h"
- #include "ProcessUtil.h"
- #include "SplashScreen.h"
- #include "StringUtil.h"
- #include "Version.h"
-
- #include "Startup.h"
-
-
- /*** LOCAL CONSTANTS ***/
-
- #define TrapMask 0x0800
- #define kGestaltMask 1L
-
- /* window info */
- #define kScreenBorder 4 /* pixels */
- #define kIconSize 32
-
-
- /*** LOCAL PROTOTYPES ***/
-
- static void startupErrors ( void );
- static void startupGestalt ( void );
- static void startupEvent ( void );
- static void startupAE ( void );
- #if kCompileWithForeground
- static void startupMenus ( void );
- static void startupWindows ( void );
- static void startupQuickDraw ( void );
- #if kCompileWithDragNDrop
- static void startupDrag ( void );
- #endif
-
- #else
- /* make the interface function prototypes null */
- #define startupMenus()
- #define startupWindows()
- #define startupQuickDraw()
- #endif /* kCompileWithForeground */
-
- static Boolean startupTrapAvailable ( unsigned long );
- static TrapType startupTrapType ( unsigned long );
-
-
- /*** FUNCTIONS ***/
-
- void
- StartupApplication ( void )
- {
- OSErr theErr;
-
- /* moved to ProcessStartup() */
- // gSleepTicks = kSleepTicks;
- // gFrontProcess = ProcessCurrentIsFront ();
- // #if kCompileWithProcessFileSpec
- // ProcessGetMyFSSpec ( &gProcessFSSpec );
- // #endif
-
- ProcessStartup ();
- startupErrors ();
-
- VersionGetShort ( 1, gVersionStr ); /* must be before splash screen */
-
- LogStartup (); /* set up the log file */
-
- SplashScreenCreate (); /* put up splash screen */
-
- startupGestalt (); /* must be before all Manager inits except toolbox and memory */
- startupEvent ();
- startupAE ();
- startupMenus ();
- ThreadStartup ();
- startupWindows (); /* must be after startupMenus */
- startupQuickDraw ();
- #if kCompileWithDragNDrop
- startupDrag ();
- #endif
- AboutBoxInit ();
-
- #if kCompileWithQuitOnLongIdle
- gDoIdleQuit = true; /* quit after given period of idle time */
- gTimeLastAction = nil; /* no action has been performed yet */
- gIdleTimeToQuit = kIdleTimeToQuit;
- gDoIdleQuitOnOpenApp = kCompileWithIdleQuitOnOpenApp;
- #endif
-
- theErr = InitCGIUtil (); /* CGI Specific */
- if ( theErr != noErr )
- {
- ErrorStartup ( kerrStartupCGI );
- }
-
- theErr = InitListSTARUtil (); /* ListSTAR Specific */
- if ( theErr != noErr )
- {
- ErrorStartup ( kerrStartupListSTAR );
- }
-
- /* • the result of this call should be checked to see whether
- initialization was successful */
- theErr = CustomStartup ();
- if ( theErr != noErr )
- {
- ErrorStartup ( kerrStartupCustomInit );
- }
-
- SplashScreenDispose (); /* remove splash screen */
- } /* StartupApplication */
-
-
- /*** Initialization Functions ***/
- #pragma mark -
-
- /* Initialize default string for system errors */
- static void
- startupErrors ( void )
- {
- StringHandle tempStr;
-
- /* set system error default string */
-
- gSystemErrorStr = (StringHandle) MemoryNewHandle ( sizeof(Str255), NULL );
- if ( gSystemErrorStr == NULL )
- {
- return;
- }
-
- HLockHi ( (Handle)gSystemErrorStr );
-
- tempStr = GetString ( krErrSystemDefault );
- if ( tempStr == NULL )
- {
- StringPascalCopy ( (char *)ksErrSystemDefault, (char *)(*gSystemErrorStr) );
- }
- else
- {
- HLock ( (Handle)tempStr );
-
- StringPascalCopy ( (char *)(*tempStr), (char *)(*gSystemErrorStr) );
-
- HUnlock ( (Handle)tempStr );
- ReleaseResource ( (Handle)tempStr );
- }
-
- HUnlock ( (Handle)gSystemErrorStr );
- } /* startupErrors */
-
-
- /* Determine if the Gestalt manager is available */
- static void
- startupGestalt ( void )
- {
- Boolean gestaltAvail;
-
- /* determine whether the Gestalt call is available */
- gestaltAvail = startupTrapAvailable ( _Gestalt );
- if ( !gestaltAvail )
- {
- /* Gestalt require for application to work, inform the user and exit */
- ErrorStartup ( kerrStartupGestalt );
- }
- } /* startupGestalt */
-
-
- /* Determine if WaitNextEvent trap is available. */
- static void
- startupEvent ( void )
- {
- OSErr theErr;
- SysEnvRec theSysEnv; /* system environment */
- Boolean WNEAvail;
-
- /* what are we running on here (System 4.1 or greater) */
- theErr = SysEnvirons ( 1, &theSysEnv );
- if ( theErr != noErr )
- {
- /* can't figure out what the system environment is,
- inform the user and exit this application */
- ErrorStartup ( kerrStartupSysEnv );
- }
- else
- {
- /* is WaitNextEvent implemented? - from Macintosh Tech Note #158 */
- if ( theSysEnv.machineType > envMachUnknown )
- {
- WNEAvail = NGetTrapAddress(0x60, ToolTrap) != NGetTrapAddress(0x9F, ToolTrap);
- }
- else
- {
- WNEAvail = false;
- }
-
- if ( !WNEAvail )
- {
- /* WaitNextEvent trap is missing - can't run */
- ErrorStartup ( kerrStartupWNE );
- }
- }
- } /* startupEvent */
-
-
- /* Determine if Apple Event Manager is present and install handlers */
- static void
- startupAE ( void )
- {
- long feature;
- OSErr theErr;
-
- theErr = Gestalt ( gestaltAppleEventsAttr, &feature );
- if ( (theErr != noErr) ||
- !(feature & (kGestaltMask << gestaltAppleEventsPresent)) )
- {
- /* Apple Events not available, inform the user and exit */
- ErrorStartup ( kerrStartupAppleEvent );
- }
-
- gAEIdleUPP = NewAEIdleProc ( AEFuncAEIdleFunc );
-
- theErr = AEInstallHandlers ();
- if ( theErr != noErr )
- {
- ErrorSystem ( theErr );
- }
- } /* startupAE */
-
-
- /* setup menus */
- #if kCompileWithForeground
- static void
- startupMenus ( void )
- {
- Handle theMenuBar;
-
- theMenuBar = GetNewMBar ( kmMenuBarID );
- if ( theMenuBar == NULL )
- {
- /* can't load menu bar */
- ErrorStartup ( kerrStartupMenu );
- }
-
- SetMenuBar ( theMenuBar );
-
- gmAppleMenu = GetMHandle ( kmAppleMenuID );
- /* add "Apple Menu Items" to the Apple menu */
- AddResMenu ( gmAppleMenu, 'DRVR' );
-
- gmFileMenu = GetMHandle ( kmFileMenuID );
- gmEditMenu = GetMHandle ( kmEditMenuID );
-
- DrawMenuBar ();
- } /* startupMenus */
- #endif /* kCompileWithForeground */
-
-
- /* Determine the space available on the main and other monitors */
- #if kCompileWithForeground
- static void
- startupWindows ( void )
- {
- short resW;
- short resH;
- short menuBarHeight;
- RgnHandle grayRgnHdl;
- Rect grayRgnBounds;
-
- /* get screen dimensions */
- resW = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
- resH = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top;
-
- menuBarHeight = LMGetMBarHeight ();
-
- /* gScreenRect is the size of the main screen, inset by a margin */
- SetRect ( &gScreenRect, kScreenBorder, menuBarHeight + kScreenBorder,
- resW - kScreenBorder, resH - kScreenBorder );
-
- /* get entire gray rgn rect (the size of all monitors) */
- grayRgnHdl = GetGrayRgn (); /* known as the 'gray region' */
- grayRgnBounds = (*grayRgnHdl)->rgnBBox; /* the bounding box for the region */
-
- resW = grayRgnBounds.right - grayRgnBounds.left;
- resH = grayRgnBounds.bottom - grayRgnBounds.top;
-
- /* this rect encompasses all of the monitor desktop space,
- minus the menu bar and inset by a margin. We'll use this
- rectangle to limit dragging and resizing windows */
- SetRect ( &gGrayRgnRect,
- /* left */ kScreenBorder,
- /* top */ menuBarHeight + kScreenBorder,
- /* right */ resW - kScreenBorder,
- /* bottom */ resH - kScreenBorder );
- } /* startupWindows */
- #endif /* kCompileWithForeground */
-
-
- /* QuickDraw - Graphics */
- #if kCompileWithForeground
- static void
- startupQuickDraw ( void )
- {
- OSErr theErr;
- long feature;
- long version;
-
- gHasColorQD = false;
-
- theErr = Gestalt ( gestaltQuickdrawFeatures, &feature );
- if ( theErr == noErr )
- {
- theErr = Gestalt ( gestaltQuickdrawVersion, &version );
- if ( (theErr == noErr) &&
- ((feature & (kGestaltMask << gestaltHasColor)) != nil) &&
- (version >= gestalt8BitQD) )
- {
- gHasColorQD = true;
- }
- }
- } /* startupQuickDraw */
- #endif /* kCompileWithForeground */
-
-
- /* Drag Manager - interapplication drag'n'drop */
- #if kCompileWithDragNDrop
- static void
- startupDrag ( void )
- {
- long feature;
- OSErr theErr;
-
- theErr = Gestalt ( gestaltDragMgrAttr, &feature );
- if ( (theErr == noErr) &&
- (feature & (kGestaltMask << gestaltDragMgrPresent)) )
- {
- gHasDragNDrop = true;
- }
- else
- {
- gHasDragNDrop = false;
- }
- } /* startupAE */
- #endif
-
-
- #pragma mark -
-
- /* Determine if a trap is available */
- static Boolean
- startupTrapAvailable ( unsigned long trap )
- {
- UniversalProcPtr theInitGrafAddr;
- UniversalProcPtr theAA6EAddr;
- Boolean trapAddrMatch;
- unsigned long numToolBoxTraps;
- TrapType theTrapType;
- unsigned long theTrapMasked;
-
- theInitGrafAddr = NGetTrapAddress ( _InitGraf, ToolTrap );
- theAA6EAddr = NGetTrapAddress ( 0xAA6E, ToolTrap );
- trapAddrMatch = theInitGrafAddr == theAA6EAddr;
-
- if ( trapAddrMatch )
- {
- numToolBoxTraps = 0x200;
- }
- else
- {
- numToolBoxTraps = 0x400;
- }
-
- theTrapType = startupTrapType ( trap );
- if ( theTrapType == ToolTrap )
- {
- theTrapMasked = trap & 0x07FF;
- if ( theTrapMasked >= numToolBoxTraps )
- {
- return false;
- }
- }
-
- return NGetTrapAddress ( _Unimplemented, ToolTrap ) !=
- NGetTrapAddress ( trap, theTrapType );
- } /* startupTrapAvailable */
-
-
- /* determine the type of the trap */
- static TrapType
- startupTrapType ( unsigned long theTrap )
- {
- /* OS traps start with A0, Tool with A8 or AA. */
- if ( (theTrap & 0x0800) == nil )
- {
- return OSTrap;
- }
- else
- {
- return ToolTrap;
- }
- } /* startupTrapType */
-
-
- /***** EOF *****/
-